package com.taxer.controller;

import com.taxer.contants.Constants;
import com.taxer.db.entity.Admin;
import com.taxer.exception.TaxerExcepiton;
import com.taxer.result.Result;
import com.taxer.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by gx on 2017/9/15.
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @RequestMapping("/hello")
    @ResponseBody
    public Result Hello(@RequestBody Admin admin) {
        Result<Admin> result = new Result<>();
        try {
            if (admin.getUsername() == null || admin.getPassword() == null){
                throw new TaxerExcepiton("参数校验失败");
            }else if (adminService.getAdminByName(admin.getUsername())==null){
                throw new TaxerExcepiton("账号密码不存在");
            }

            else if(admin.getUsername().equals(adminService.getAdminByName(admin.getUsername()).getUsername())&&admin.getPassword().equals(adminService.getAdminByName(admin.getUsername()).getPassword())){
            result.setData(adminService.getAdminByName(admin.getUsername()));
            result.setMessage("登录成功");
            }else if(admin.getUsername().equals(adminService.getAdminByName(admin.getUsername()).getUsername())&&admin.getPassword().equals(adminService.getAdminByName(admin.getUsername()).getPassword())==false){
                result.setMessage("密码错误");

            }
        } catch (TaxerExcepiton taxerExcepiton) {
            result.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
            result.setMessage(taxerExcepiton.getMessage());
        }
        return result;

    }
    @RequestMapping("/test")
    @ResponseBody
    public String test(){
        try {
            System.out.println(adminService.getAdminByName("root"));
        } catch (TaxerExcepiton taxerExcepiton) {
            taxerExcepiton.printStackTrace();
        }
        return "hello";
    }
}
